import { Injectable } from '@angular/core'

@Injectable()
export class PageService {
  public total: number = 0 // 数据数
  public pageIndex = 0
  public pageSize = 10
  public loading: boolean = false
  public totalPage: number // 总页数
  public complete = false
  public pageSizes = [10, 20, 30, 40]

  constructor() {}
  /**
   * 获取分页配置信息
   */
  public getConfig() {
    return {
      index: this.pageIndex,
      size: this.pageSize
    }
  }

  public next() {
    if (!this.complete) {
      this.pageIndex += 1
      this.checkComplete()
    }
  }

  public reset() {
    this.pageIndex = 1
    this.loading = false
    this.complete = false
  }

  /**
   * 更新分页配置信息
   * @param total 数据总数
   */
  public update({ rowCount: total }) {
    this.total = total
    this.totalPage = Math.ceil(this.total / this.pageSize)
    this.checkComplete()
  }

  private checkComplete() {
    if (this.pageIndex >= this.totalPage) {
      this.complete = true
    }
  }

  public change({ type, pi, ps }) {
    if (['pi', 'ps'].includes(type)) {
      this.pageIndex = pi
      this.pageSize = ps
      return true
    }
  }
}
